﻿using Prism.Commands;
using Prism.Events;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Threading;
using XH.DataUpload.Client.Global;
using XH.DataUpload.Logger;
using static XH.DataUpload.Logger.Logger;

namespace XH.DataUpload.Client.ViewModels
{
    public class MainViewModel
    {
        private DispatcherTimer timer = new DispatcherTimer(); // 定时器
        private readonly IDialogService _dialogService;
        private readonly IRegionManager _regionManager;
        private readonly IEventAggregator _eventAggregator;
        public MainViewModel(
            IDialogService dialogService,
            IRegionManager regionManager,
            IEventAggregator eventAggregator)
        {
            _dialogService = dialogService;
            _regionManager = regionManager;
            _eventAggregator = eventAggregator;
            // 先打开页面
            _dialogService.ShowDialog("LoginView", res =>
            {
                if (res.Result != ButtonResult.OK) Environment.Exit(0);
                else
                {
                    _regionManager.RegisterViewWithRegion("MainRegion", "UserMainView");
                }
            });
            ShowMainRegionCommand = new DelegateCommand<string>(DoShowMainRegion) { };


            timer.IsEnabled = true;
            timer.Interval = new TimeSpan(0, 0, 0, 1);
            timer.Tick += Timer_Tick;

            // 订阅无参
            _eventAggregator.GetEvent<EventMessage>().Subscribe(OpenShowView);

        }

        private void OpenShowView(string obj)
        {
            DoShowMainRegion(obj);
        }

        #region MainTCommand
        public DelegateCommand<string> ShowMainRegionCommand { get; set; }
        #endregion

        #region CommandMethod
        /// <summary>
        /// 界面跳转
        /// </summary>
        /// <param name="obj"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void DoShowMainRegion(string obj)
        {
            //在MainRegion中打开对应的obj
            if (obj == "Production Mode" ||
                obj == "Engineering Mode" ||
                obj == "GRR Mode" ||
                obj == "CPK Mode")
                _regionManager.Regions["MainRegion"].RequestNavigate("UserEngineeringView");
            else
                _regionManager.Regions["MainRegion"].RequestNavigate(obj);
        }

        #endregion

        /// <summary>
        /// 当前页面的定时器 写Log 使用
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void Timer_Tick(object? sender, EventArgs e)
        {
            try
            {
                #region ORM_log
                if (LogQueue.ORMLogQueue.Count > 0)
                {
                    LogQueue.ORMLogQueue.TryDequeue(out string message);
                    string ORM_Log_Path = $@"{FilePath.LogPath}\ORM\";
                    Logger.Logger.WirteLog(ORM_Log_Path, message, LogLevel.Info);
                }
                #endregion
                #region Exception_log
                if (LogQueue.ExceptionLogQueue.Count > 0)
                {
                    LogQueue.ExceptionLogQueue.TryDequeue(out string message);
                    string Exception_Log_Path = $@"{FilePath.LogPath}\Exception\";
                    Logger.Logger.WirteLog(Exception_Log_Path, message, LogLevel.Error);
                }
                #endregion
            }
            catch (Exception ex)
            {
                LogQueue.ExceptionLogQueue.Enqueue(ex.Message);
            }
        }
    }
}
